 <template>
  <div class="app-container">
    <el-form label-width="120px">
      <el-form-item label="医院名称">
        <el-input v-model="hospset.hosname" />
      </el-form-item>
      <el-form-item label="医院编号">
        <el-input v-model="hospset.hoscode" />
      </el-form-item>
      <el-form-item label="api地址">
        <el-input v-model="hospset.apiUrl" />
      </el-form-item>
      <el-form-item label="联系人">
        <el-input v-model="hospset.contactsName" />
      </el-form-item>
      <el-form-item label="电话">
        <el-input v-model="hospset.contactsPhone" />
      </el-form-item>
      <el-form-item>
        <el-button
          :disabled="saveBtnDisabled"
          type="primary"
          @click="saveOrUpdate"
          >保存</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import hosp from "@/api/hosp";

export default {
  data() {
    return {
      saveBtnDisabled: false, //是否禁用保存按钮
      hospset: {}, //医院设置的信息
    };
  },
  created() {
    //通过id,判断是修改还是添加
    this.hospset.id = this.$route.params.id;
    if (this.hospset.id) {
      //有id是修改,回显

      this.fetchData();
    }
  },
  methods: {
    //添加功能
    saveOrUpdate() {
      this.saveBtnDisabled = true;

      if (this.hospset.id) {
        this.update();
      } else {
        this.save();
      }
    },

    //判断当前是添加还是修改

    save() {
      //执行添加的请求
      hosp.add(this.hospset).then((response) => {
        this.$message.success("添加成功");
        //使用路由跳转到列表页面
        this.$router.push("/yygh/hospset/list");
      });
    },

    update() {
      //执行修改

      hosp.updateById(this.hospset).then((response) => {
        this.$message.success("修改成功");
        //使用路由跳转到列表页面
        this.$router.push("/yygh/hospset/list");});
    },
    //获取数据
    fetchData() {
      hosp.getById(this.hospset.id).then((response) => {
        this.hospset = response.data.item;
      });
    }

  }
}
</script>
